package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * [圈子] 模块
 */
@RequestMapping("movements")
@RestController
public class MovementsController {

    @Autowired
    private MovementsService movementsService;
    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity<Void> saveMovements(@RequestParam("textContent") String textContent,
                                              @RequestParam("location") String location,
                                              @RequestParam("longitude") String longitude,
                                              @RequestParam("latitude") String latitude,
                                              @RequestParam("imageContent") MultipartFile[] imageContent) throws IOException {
        this.movementsService.saveMovements(textContent, location, longitude, latitude, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友的动态信息
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pageResult = this.movementsService.queryPublishList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询推荐的动态信息
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pageResult = this.movementsService.queryRecommendPublishList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 点赞动态
     */
    @GetMapping("/{publishId}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("publishId") String publishId) {
        Long count = this.movementsService.likeComment(publishId);
        if (null != count) {
            //发送消息
            this.quanziMQService.sendLikePublishMsg(publishId);
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{publishId}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("publishId") String publishId) {
        Long count = this.movementsService.disLikeComment(publishId);
        if (null != count) {
            //发送消息
            this.quanziMQService.sendDisLikePublishMsg(publishId);
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 喜欢
     */
    @GetMapping("/{publishId}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("publishId") String publishId) {
        Long count = this.movementsService.loveComment(publishId);
        if (null != count) {
            //发送消息
            this.quanziMQService.sendLovePublishMsg(publishId);
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{publishId}/unlove")
    public ResponseEntity<Long> unLoveComment(@PathVariable("publishId") String publishId) {
        Long count = this.movementsService.unLoveComment(publishId);
        if (null != count) {
            //发送消息
            this.quanziMQService.sendDisLovePublishMsg(publishId);
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 查询单条动态信息
     */
    @GetMapping("{publishId}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("publishId") String publishId) {
        Movements movements = this.movementsService.queryMovementsById(publishId);
        if (null != movements) {
            //发送消息
            this.quanziMQService.sendQueryPublishMsg(publishId);
        }
        return ResponseEntity.ok(movements);
    }


    /**
     * 谁看过我
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList() {
        List<VisitorsVo> list = this.movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }

    /**
     * 自己的所有动态
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize,
                                                     @RequestParam(value = "userId") Long userId) {
        PageResult pageResult = this.movementsService.queryAlbumList(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
}
